<script setup name="BizOrderIndex">
import eventBus from "@/utils/eventBus";
import { usePagination } from "@/utils/composables";
import FormMode from "./modules/FormMode/index.vue";
import { parseTime, selectDictLabel, addDateRange } from "@/utils/aidex";
import { listBizOrder, pushOrder } from "@/api/biz/bizOrder";
import { listBizOrderDetails } from "@/api/biz/bizOrderDetails";
import { getDicts } from "@/api/system/dict/data";
const { pagination, getParams, onSizeChange, onCurrentChange, onSortChange } =
  usePagination();
const router = useRouter();

const data = ref({
  loading: false,
  /**
   * 详情展示模式
   * router 路由跳转
   * dialog 对话框
   * drawer 抽屉
   */
  formMode: "drawer",
  // 详情
  formModeProps: {
    visible: false,
    id: ""
  },
  // 搜索
  search: {
    searchDate: [],
    orderId: "",
    lianxiName: "",
    phone: "",
    orderType: "0",
    status: "-99"
  },
  // 批量操作
  batch: {
    enable: false,
    selectionDataList: []
  },
  // 列表数据
  dataList: [],
  // 订单详情列表
  orderDetailsList: [],
  // 字典
  orderStatusOptions: []
});

// 页面挂载时 调用
onMounted(() => {
  getDataList();
  if (data.value.formMode === "router") {
    eventBus.on("get-data-list", () => {
      getDataList();
    });
  }
  getDicts("order_status_xihu").then((res) => {
    data.value.orderStatusOptions = res.data;
  });
});
//
onBeforeUnmount(() => {
  if (data.value.formMode === "router") {
    eventBus.off("get-data-list");
  }
});

// 每页数量切换
function sizeChange(size) {
  onSizeChange(size).then(() => getDataList());
}

// 当前页码切换（翻页）
function currentChange(page = 1) {
  onCurrentChange(page).then(() => getDataList());
}

// 字段排序
function sortChange(prop, order) {
  onSortChange(prop, order).then(() => getDataList());
}
// 查询
function getDataList() {
  data.value.loading = true;
  let params = getParams();
  data.value.search.orderType &&
    (params.orderType = data.value.search.orderType);
  data.value.search.lianxiName &&
    (params.lianxiName = data.value.search.lianxiName);
  data.value.search.phone && (params.phone = data.value.search.phone);
  data.value.search.orderId && (params.orderId = data.value.search.orderId);
  data.value.search.status && (params.status = data.value.search.status);

  listBizOrder(addDateRange(params, data.value.search.searchDate)).then(
    (res) => {
      data.value.loading = false;
      data.value.dataList = res.data.list;
      pagination.value.total = res.data.total;
    }
  );
}

// 编辑
function onEdit(row) {
  data.value.formMode = "drawer";
  if (data.value.formMode === "router") {
    router.push({
      name: "routerName",
      params: {
        id: row.id
      }
    });
  } else {
    data.value.formModeProps.id = row.id;
    data.value.formModeProps.visible = true;
  }
}
// 添加
function onAdd() {
  data.value.formModeProps.id = "";
  data.value.formMode = "dialog";
  data.value.formModeProps.visible = true;
}
// 批量删除 单独删除
function onDel(row) {
  const ids =
    row.id ||
    data.value.batch.selectionDataList.map((item) => {
      return item.id;
    });
  const names =
    row.postName ||
    data.value.batch.selectionDataList.map((item) => {
      return item.postName;
    });
  if (ids.length > 0) {
    ElMessageBox.confirm("确认删除「" + names + "」吗？", "确认信息")
      .then(() => {
        delPost(ids).then((res) => {
          getDataList();
          if (res.code == 200)
            ElMessage.success({
              message: "删除成功",
              center: true
            });
        });
      })
      .catch(() => {});
  } else {
    ElMessage.warning({ message: "请选择数据", center: true });
  }
}
function load(row) {
  listBizOrderDetails({ orderId: row.id }).then((res) => {
    data.value.orderDetailsList = res.data.list;
  });
}
function orderStatusOptionsFormat(row) {
  if (row.status) {
    return selectDictLabel(data.value.orderStatusOptions, row.status);
  } else {
    return "";
  }
}
function handlePushOrder(row) {
  pushOrder(row.id).then((res) => {
    if (res.data) {
      ElMessage.success({
        message: "订单推送成功",
        center: true
      });
    }
  });
}
</script>

<template>
  <div>
    <page-main style="height: calc(100vh - 160px); margin: 10px">
      <search-bar>
        <el-form
          :model="data.search"
          size="default"
          label-width="auto"
          label-suffix=""
        >
          <el-row :gutter="30">
            <el-col :span="5">
              <el-form-item label="订单编号">
                <el-input
                  v-model="data.search.orderId"
                  placeholder="请输入订单编号"
                  clearable
                  @keydown.enter="currentChange()"
                  @clear="currentChange()"
                />
              </el-form-item>
            </el-col>

            <el-col :span="5">
              <el-form-item label="收货人">
                <el-input
                  v-model="data.search.lianxiName"
                  placeholder="请输入收货人"
                  clearable
                  @keydown.enter="currentChange()"
                  @clear="currentChange()"
                />
              </el-form-item>
            </el-col>

            <el-col :span="5">
              <el-form-item label="联系电话">
                <el-input
                  v-model="data.search.phone"
                  placeholder="请输入联系电话"
                  clearable
                  @keydown.enter="currentChange()"
                  @clear="currentChange()"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="订单时间">
                <el-date-picker
                  v-model="data.search.searchDate"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  value-format="YYYY-MM-DD"
                />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="订单状态">
                <el-select v-model="data.search.status">
                  <el-option
                    :key="index"
                    v-for="(item, index) in data.orderStatusOptions"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                    >{{ item.dictLabel }}</el-option
                  >
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="5" style="text-align: right">
              <el-form-item>
                <el-button type="primary" @click="currentChange()">
                  <template #icon>
                    <el-icon>
                      <svg-icon name="ep:search" />
                    </el-icon>
                  </template>
                  查询
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </search-bar>

      <div style="height: calc(100vh - 380px); overflow-y: auto">
        <el-table
          ref="table"
          v-loading="data.loading"
          class="list-table"
          :data="data.dataList"
          border
          highlight-current-row
          row-key="id"
          @sort-change="sortChange"
          @selection-change="data.batch.selectionDataList = $event"
          @expand-change="load"
        >
          <!-- <el-table-column type="selection" align="center" />
          -->
          <el-table-column type="expand" align="left">
            <template #default="props" class="data-table">
              <el-row style="height: 40px; line-height: 40px">
                <el-col :span="1"><span></span></el-col>
                <el-col :span="10" style="font-size: 14px; font-weight: 400">
                  <span>订单详情</span>
                </el-col>
                <el-col :span="2">
                  <span></span>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="1">
                  <span></span>
                </el-col>
                <el-col :span="15">
                  <el-table :data="data.orderDetailsList" border width="100%">
                    <el-table-column label="产品名称" prop="productName" />
                    <el-table-column label="产品照片" prop="image" width="120">
                      <template v-slot="scope">
                        <img width="60" height="60" :src="scope.row.image" />
                      </template>
                    </el-table-column>
                    <el-table-column label="货号/69码" prop="upc" width="180" />
                    <el-table-column label="规格" prop="unit" width="180" />
                    <el-table-column
                      label="原价"
                      prop="price"
                      align="right"
                      width="140"
                    >
                      <template v-slot="scope">
                        ¥{{ parseFloat(scope.row.price).toFixed(2) }}
                      </template>
                    </el-table-column>
                    <el-table-column
                      label="折扣价"
                      prop="salePrice"
                      align="right"
                      width="140"
                    >
                      <template v-slot="scope">
                        ¥{{ parseFloat(scope.row.salePrice).toFixed(2) }}
                      </template>
                    </el-table-column>
                    <el-table-column
                      label="数量"
                      prop="count"
                      align="right"
                      width="140"
                    />
                    <el-table-column
                      label="小计"
                      prop="amount"
                      align="right"
                      width="180"
                    >
                      <template v-slot="scope">
                        ¥{{ parseFloat(scope.row.amount).toFixed(2) }}
                      </template>
                    </el-table-column>
                  </el-table>
                  <el-col :span="2">
                    <span></span>
                  </el-col>
                </el-col>
              </el-row>
              <el-row style="height: 20px">
                <el-col :span="2"><span></span></el-col>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column
            type="index"
            align="center"
            label="序号"
            width="60"
          />
          <el-table-column
            prop="orderId"
            label="订单编号"
            show-overflow-tooltip
            width="180"
            align="center"
          >
            <template v-slot="scope">
              {{ scope.row.orderId }}
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="订单时间"
            show-overflow-tooltip
            width="170"
            align="center"
          >
            <template v-slot="scope">
              {{ scope.row.createTime }}
            </template>
          </el-table-column>
          <el-table-column
            prop="status"
            label="订单状态"
            show-overflow-tooltip
            width="120"
            align="center"
          >
            <template v-slot="scope">
              <el-tag>
                {{ orderStatusOptionsFormat(scope.row) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="lianxiName"
            label="收货人"
            show-overflow-tooltip
            width="120"
            align="center"
          >
            <template v-slot="scope">
              {{ scope.row.lianxiName }}
            </template>
          </el-table-column>
          <el-table-column
            prop="phone"
            label="电话号码"
            show-overflow-tooltip
            width="120"
            align="center"
          >
            <template v-slot="scope">
              {{ scope.row.phone }}
            </template>
          </el-table-column>

          <el-table-column
            prop="roomAddress"
            label="地址"
            show-overflow-tooltip
            width="120"
            align="center"
          >
            <template v-slot="scope">
              {{ scope.row.roomAddress }}
            </template>
          </el-table-column>

          <el-table-column
            prop="amount"
            label="订单金额"
            show-overflow-tooltip
            width="120"
            align="right"
          >
            <template v-slot="scope">
              {{ parseFloat(scope.row.amount).toFixed(2) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="saleAmount"
            label="折后价格"
            show-overflow-tooltip
            width="120"
            align="right"
          >
            <template v-slot="scope">
              {{ parseFloat(scope.row.saleAmount).toFixed(2) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="payAmount"
            label="支付金额"
            show-overflow-tooltip
            width="120"
            align="right"
          >
            <template v-slot="scope">
              {{ parseFloat(scope.row.payAmount).toFixed(2) }}
            </template>
          </el-table-column>

          <el-table-column
            prop="payStatus"
            label="支付状态"
            show-overflow-tooltip
            width="120"
            align="center"
          >
            <template v-slot="scope">
              <el-tag v-if="scope.row.payStatus == '1'" type="success"
                >已支付</el-tag
              >
              <el-tag v-else type="warning">待支付</el-tag>
            </template>
          </el-table-column>

          <el-table-column
            prop="isVoucher"
            label="使用优惠券"
            show-overflow-tooltip
            width="120"
            align="center"
          >
            <template v-slot="scope">
              <el-tag v-if="scope.row.isVoucher == '2'" type="success"
                >使用</el-tag
              >
              <el-tag v-else>不使用</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="voucherPrice"
            label="优惠券面值"
            show-overflow-tooltip
            width="120"
            align="right"
          >
            <template v-slot="scope">
              {{
                parseFloat(
                  scope.row.voucherPrice ? scope.row.voucherPrice : 0
                ).toFixed(2)
              }}
            </template>
          </el-table-column>

          <el-table-column
            prop="orderInType"
            label="是否上门"
            show-overflow-tooltip
            width="120"
            align="center"
          >
            <template v-slot="scope">
              <el-tag v-if="scope.row.orderInType == '2'" type="success"
                >自行投放</el-tag
              >
              <el-tag v-else>上门取</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="isInsure"
            label="是否保价"
            show-overflow-tooltip
            width="120"
            align="center"
          >
            <template v-slot="scope">
              <el-tag v-if="scope.row.isInsure == '2'" type="success"
                >保价</el-tag
              >
              <el-tag v-else>不保价</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="insurePrice"
            label="保价金额"
            show-overflow-tooltip
            width="120"
            align="right"
          >
            <template v-slot="scope">
              {{
                parseFloat(
                  scope.row.insurePrice ? scope.row.insurePrice : 0
                ).toFixed(2)
              }}
            </template>
          </el-table-column>
          <el-table-column
            prop="declarePrice"
            label="声明价格"
            show-overflow-tooltip
            width="120"
            align="right"
          >
            <template v-slot="scope">
              {{
                parseFloat(
                  scope.row.declarePrice ? scope.row.declarePrice : 0
                ).toFixed(2)
              }}
            </template>
          </el-table-column>
          <el-table-column
            prop="payDate"
            label="支付时间"
            show-overflow-tooltip
            width="120"
            align="center"
          >
            <template v-slot="scope">
              {{ scope.row.payDate }}
            </template>
          </el-table-column>
          <el-table-column
            prop="remark"
            label="备注"
            show-overflow-tooltip
            width="120"
            align="center"
          >
            <template v-slot="scope">
              {{ scope.row.remark }}
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            width="160"
            align="center"
            fixed="right"
          >
            <template #default="scope">
              <el-button-group>
                <el-button
                  type="primary"
                  size="small"
                  text
                  @click="handlePushOrder(scope.row)"
                >
                  <template #icon>
                    <el-icon>
                      <svg-icon name="ant-design:edit-filled" />
                    </el-icon>
                  </template>
                  确认退款</el-button
                >

                <!-- <el-button
                  type="danger"
                  text
                  size="small"
                  @click="onDel(scope.row)"
                >
                  <template #icon>
                    <el-icon>
                      <svg-icon name="ep:close-bold" />
                    </el-icon>
                  </template>
                  删除
                </el-button> -->
              </el-button-group>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-pagination
        :current-page="pagination.page"
        :total="pagination.total"
        :page-size="pagination.size"
        :page-sizes="pagination.sizes"
        :layout="pagination.layout"
        :hide-on-single-page="false"
        class="pagination"
        background
        @size-change="sizeChange"
        @current-change="currentChange"
      />
    </page-main>
    <FormMode
      v-if="['dialog', 'drawer'].includes(data.formMode)"
      :id="data.formModeProps.id"
      v-model="data.formModeProps.visible"
      :mode="data.formMode"
      @success="getDataList"
    />
  </div>
</template>

<style lang="scss" scoped>
.el-pagination {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
